package cn.harry.cabinet.theme.manager

import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout
import cn.harry.cabinet.R
import cn.harry.cabinet.theme.constants.ThemeConstants
import com.google.android.material.navigation.NavigationView

/**
 * 导航菜单主题管理器 - 重构版本
 * 专门负责导航菜单的主题切换和样式调整
 * 
 * 特性：
 * - 导航头部主题化
 * - 菜单项动态样式
 * - 工具栏主题适配
 * - 抽屉布局背景
 */
object NavigationThemeManager {
    
    /**
     * 主题配色方案定义
     */
    data class NavigationThemeColors(
        // 主要颜色
        val primaryColor: Int,
        val primaryDarkColor: Int,
        val accentColor: Int,
        val secondaryColor: Int,
        
        // 背景颜色
        val headerBackgroundStart: Int,
        val headerBackgroundEnd: Int,
        val drawerBackground: Int,
        val toolbarBackground: Int,
        
        // 文字颜色
        val primaryTextColor: Int,
        val secondaryTextColor: Int,
        val menuTextColor: Int,
        val menuTextSelectedColor: Int,
        
        // 图标颜色
        val iconColor: Int,
        val iconSelectedColor: Int,
        val avatarIconColor: Int,
        
        // 装饰颜色
        val decorationColor1: Int,
        val decorationColor2: Int,
        val decorationColor3: Int,
        val statusIndicatorColor: Int,
        
        // 按钮颜色
        val buttonBackgroundColor: Int,
        val buttonTextColor: Int,
        val dividerColor: Int
    )
    
    /**
     * 获取主题的导航配色方案
     */
    fun getNavigationThemeColors(theme: ThemeManager.Theme, @Suppress("UNUSED_PARAMETER") _context: Context): NavigationThemeColors {
        return when (theme) {
            ThemeManager.Theme.CHINA_RED -> NavigationThemeColors(
                primaryColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryColorRes),
                primaryDarkColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryDarkColorRes),
                accentColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.accentColorRes),
                secondaryColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.secondaryColorRes),
                
                headerBackgroundStart = ContextCompat.getColor(_context, R.color.china_red_background),
                headerBackgroundEnd = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.secondaryColorRes),
                drawerBackground = ContextCompat.getColor(_context, R.color.china_red_background),
                toolbarBackground = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryDarkColorRes),
                
                primaryTextColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                secondaryTextColor = ContextCompat.getColor(_context, R.color.tech_text_secondary),
                menuTextColor = ContextCompat.getColor(_context, R.color.tech_text_secondary),
                menuTextSelectedColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                
                iconColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                iconSelectedColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                avatarIconColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryColorRes),
                
                decorationColor1 = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryLightColorRes),
                decorationColor2 = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.accentColorRes),
                decorationColor3 = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.secondaryColorRes),
                statusIndicatorColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryColorRes),
                
                buttonBackgroundColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryColorRes),
                buttonTextColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                dividerColor = ContextCompat.getColor(_context, ThemeConstants.ChinaRedColors.primaryDarkColorRes)
            )
            
            ThemeManager.Theme.VIBRANT_ORANGE -> NavigationThemeColors(
                primaryColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryColorRes),
                primaryDarkColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryDarkColorRes),
                accentColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.accentColorRes),
                secondaryColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.secondaryColorRes),
                
                headerBackgroundStart = ContextCompat.getColor(_context, R.color.vibrant_orange_background),
                headerBackgroundEnd = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.secondaryColorRes),
                drawerBackground = ContextCompat.getColor(_context, R.color.vibrant_orange_background),
                toolbarBackground = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryDarkColorRes),
                
                primaryTextColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                secondaryTextColor = ContextCompat.getColor(_context, R.color.tech_text_secondary),
                menuTextColor = ContextCompat.getColor(_context, R.color.tech_text_secondary),
                menuTextSelectedColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                
                iconColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                iconSelectedColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                avatarIconColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryColorRes),
                
                decorationColor1 = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryLightColorRes),
                decorationColor2 = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.accentColorRes),
                decorationColor3 = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.secondaryColorRes),
                statusIndicatorColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryColorRes),
                
                buttonBackgroundColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryColorRes),
                buttonTextColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                dividerColor = ContextCompat.getColor(_context, ThemeConstants.VibrantOrangeColors.primaryDarkColorRes)
            )
            
            ThemeManager.Theme.TECH_BLUE -> NavigationThemeColors(
                primaryColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryColorRes),
                primaryDarkColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryDarkColorRes),
                accentColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.accentColorRes),
                secondaryColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.secondaryColorRes),
                
                headerBackgroundStart = ContextCompat.getColor(_context, R.color.tech_blue_background),
                headerBackgroundEnd = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.secondaryColorRes),
                drawerBackground = ContextCompat.getColor(_context, R.color.tech_blue_background),
                toolbarBackground = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryDarkColorRes),
                
                primaryTextColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                secondaryTextColor = ContextCompat.getColor(_context, R.color.tech_text_secondary),
                menuTextColor = ContextCompat.getColor(_context, R.color.tech_text_secondary),
                menuTextSelectedColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                
                iconColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                iconSelectedColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                avatarIconColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryColorRes),
                
                decorationColor1 = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryLightColorRes),
                decorationColor2 = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.accentColorRes),
                decorationColor3 = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.secondaryColorRes),
                statusIndicatorColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryColorRes),
                
                buttonBackgroundColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryColorRes),
                buttonTextColor = ContextCompat.getColor(_context, ThemeConstants.CommonColors.white),
                dividerColor = ContextCompat.getColor(_context, ThemeConstants.TechBlueColors.primaryDarkColorRes)
            )
        }
    }
    
    /**
     * 应用导航主题
     */
    fun applyNavigationTheme(activity: Activity, theme: ThemeManager.Theme) {
        try {

            val colors = getNavigationThemeColors(theme, activity)
            
            // 更新抽屉布局
            updateDrawerLayout(activity, colors)
            
            // 更新工具栏
            updateToolbar(activity, colors)
            
            // 更新导航菜单
            updateNavigationView(activity, colors, theme)
            
            // 更新导航头部
            updateNavigationHeader(activity, colors, theme)
            
            // 更新导航容器背景
            updateNavigationContainer(activity, theme)
            

        } catch (e: Exception) {
            android.util.Log.w(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER,
                "Failed to apply navigation theme: ${e.message}")
        }
    }
    
    /**
     * 更新抽屉布局主题
     */
    private fun updateDrawerLayout(activity: Activity, colors: NavigationThemeColors) {
        try {
            val drawerLayout = activity.findViewById<DrawerLayout>(R.id.drawer_layout)
            drawerLayout?.setBackgroundColor(colors.drawerBackground)
        } catch (e: Exception) {
            android.util.Log.d(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER,
                "Drawer layout update failed: ${e.message}")
        }
    }
    
    /**
     * 更新导航容器背景
     */
    private fun updateNavigationContainer(activity: Activity, theme: ThemeManager.Theme) {
        try {
            val navigationContainer = activity.findViewById<android.widget.LinearLayout>(R.id.navigation_container)
            val backgroundDrawable = when (theme) {
                ThemeManager.Theme.CHINA_RED -> 
                    ContextCompat.getDrawable(activity, R.drawable.china_red_nav_background)
                ThemeManager.Theme.VIBRANT_ORANGE -> 
                    ContextCompat.getDrawable(activity, R.drawable.vibrant_orange_nav_background)
                ThemeManager.Theme.TECH_BLUE -> 
                    ContextCompat.getDrawable(activity, R.drawable.tech_blue_nav_background)
            }
            navigationContainer?.background = backgroundDrawable
        } catch (e: Exception) {
            android.util.Log.d(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER,
                "Navigation container update failed: ${e.message}")
        }
    }
    
    /**
     * 更新工具栏主题
     */
    private fun updateToolbar(activity: Activity, colors: NavigationThemeColors) {
        try {
            val toolbar = activity.findViewById<Toolbar>(R.id.toolbar)
            toolbar?.let { 
                it.setBackgroundColor(colors.toolbarBackground)
                it.setTitleTextColor(colors.primaryTextColor)
                
                // 强制设置导航图标（汉堡菜单）为白色
                it.navigationIcon?.let { navIcon ->
                    navIcon.setTint(ContextCompat.getColor(activity, ThemeConstants.CommonColors.white))
                }
                
                // 设置工具栏图标颜色为白色
                it.overflowIcon?.let { overflowIcon ->
                    overflowIcon.setTint(ContextCompat.getColor(activity, ThemeConstants.CommonColors.white))
                }
            }
        } catch (e: Exception) {
            android.util.Log.d(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER,
                "Toolbar update failed: ${e.message}")
        }
    }
    
    /**
     * 更新导航菜单主题
     */
    private fun updateNavigationView(activity: Activity, colors: NavigationThemeColors, theme: ThemeManager.Theme) {
        try {
            val navigationView = activity.findViewById<NavigationView>(R.id.navigation_view)
            navigationView?.let { nav ->
                // 创建菜单项文字颜色状态列表
                val textColorStateList = ColorStateList(
                    arrayOf(
                        intArrayOf(android.R.attr.state_checked),
                        intArrayOf()
                    ),
                    intArrayOf(
                        colors.menuTextSelectedColor,
                        colors.menuTextColor
                    )
                )
                
                // 创建菜单项图标颜色状态列表
                val iconColorStateList = ColorStateList(
                    arrayOf(
                        intArrayOf(android.R.attr.state_checked),
                        intArrayOf()
                    ),
                    intArrayOf(
                        colors.iconSelectedColor,
                        colors.iconColor
                    )
                )
                
                // 应用颜色状态列表
                nav.itemTextColor = textColorStateList
                nav.itemIconTintList = iconColorStateList
                
                // 设置菜单项背景色
                nav.setBackgroundColor(Color.TRANSPARENT)
                
                // 根据主题设置菜单项背景
                val selectedDrawable = when (theme) {
                    ThemeManager.Theme.CHINA_RED -> 
                        ContextCompat.getDrawable(activity, ThemeConstants.ChinaRedColors.selectedMenuItemDrawable)
                    ThemeManager.Theme.VIBRANT_ORANGE -> 
                        ContextCompat.getDrawable(activity, ThemeConstants.VibrantOrangeColors.selectedMenuItemDrawable)
                    ThemeManager.Theme.TECH_BLUE -> 
                        ContextCompat.getDrawable(activity, ThemeConstants.TechBlueColors.selectedMenuItemDrawable)
                }
                
                selectedDrawable?.let { nav.itemBackground = it }
            }
        } catch (e: Exception) {
            android.util.Log.d(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER, 
                "NavigationView update failed: ${e.message}")
        }
    }
    
    /**
     * 更新导航头部主题
     */
    private fun updateNavigationHeader(activity: Activity, colors: NavigationThemeColors, @Suppress("UNUSED_PARAMETER") _theme: ThemeManager.Theme) {
        try {
            val navigationView = activity.findViewById<NavigationView>(R.id.navigation_view)
            val headerView = navigationView?.getHeaderView(0)
            
            headerView?.let { header ->
                // 设置头部背景渐变
                val gradientDrawable = android.graphics.drawable.GradientDrawable(
                    android.graphics.drawable.GradientDrawable.Orientation.LEFT_RIGHT,
                    intArrayOf(colors.headerBackgroundStart, colors.headerBackgroundEnd)
                )
                header.background = gradientDrawable
                
                // 更新头部文字颜色
                updateHeaderTexts(header, colors)
                
                // 更新头部图标
                updateHeaderIcons(header, colors)
            }
        } catch (e: Exception) {
            android.util.Log.d(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER, 
                "Navigation header update failed: ${e.message}")
        }
    }
    
    /**
     * 更新头部文字颜色
     */
    private fun updateHeaderTexts(headerView: View, colors: NavigationThemeColors) {
        try {
            // 递归查找所有TextView并更新颜色
            fun updateTextViews(view: View) {
                when (view) {
                    is TextView -> view.setTextColor(colors.primaryTextColor)
                    is android.view.ViewGroup -> {
                        for (i in 0 until view.childCount) {
                            updateTextViews(view.getChildAt(i))
                        }
                    }
                }
            }
            updateTextViews(headerView)
        } catch (e: Exception) {
            android.util.Log.d(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER, 
                "Header text update failed: ${e.message}")
        }
    }
    
    /**
     * 更新头部图标颜色
     */
    private fun updateHeaderIcons(headerView: View, colors: NavigationThemeColors) {
        try {
            // 递归查找所有ImageView并更新颜色
            fun updateImageViews(view: View) {
                when (view) {
                    is ImageView -> {
                        view.imageTintList = ColorStateList.valueOf(colors.avatarIconColor)
                    }
                    is android.view.ViewGroup -> {
                        for (i in 0 until view.childCount) {
                            updateImageViews(view.getChildAt(i))
                        }
                    }
                }
            }
            updateImageViews(headerView)
        } catch (e: Exception) {
            android.util.Log.d(ThemeConstants.LogTags.NAVIGATION_THEME_MANAGER, 
                "Header icon update failed: ${e.message}")
        }
    }
}